<?php
/**
 +------------------------------------------------------------------------------
 * 文章发布
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2015-3-2
 +------------------------------------------------------------------------------
 */
namespace Admin\Controller;

class ArticleController extends AdminController{
    
    /**
     * @todo 分类列表
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function category() {
    	$ask_category_pid  = (int)I('get.pid',0);
    	$ask_category_name = strip_tags(trim(I('get.search_text')));
    	
    	if($ask_category_pid > 0){
    	    $obj = D('ServiceApi');
    	    $data = $obj->getAskCategory($ask_category_pid);
    	    $this->assign('data',$data);
    	    
    		$data_up = $data['ask_category_pid'] > 0 ? $obj->getAskCategory($data['ask_category_pid']) : null;
			$this->assign('data_up',$data_up);
    	}else{
    		$this->assign('data',NULL);
    	}
    	
    	//标志位筛选
    	$map['status'] = array('gt',-1);
    	
    	//层级筛选
    	$map['ask_category_pid'] = $ask_category_pid ; //update by fzh
    	
    	//文本框搜索
	    if(!empty($ask_category_name)){
    		$map['ask_category_name'] = array('like','%'.(string)$ask_category_name.'%');
    	}
    	
    	//取数据
    	$field = 'ask_category_id,ask_category_pid,ask_category_name,ask_category_title,ask_category_keyword,ask_category_description,status';
    	$list = $this->lists('AskCategory',$map,'sort desc,ask_category_id desc',$field);
    	
    	// 记录当前列表页的cookie
    	Cookie('__forward__',$_SERVER['REQUEST_URI']);
    	$this->meta_title = '文章分类列表';
    	$this->assign('_list',$list);
    	$this->display();
    }
    
    /**
     * @todo 新增分类
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function addCategory() {
    	if(IS_POST){
    		$this->updateCategory();
    	}else{
    		$pid = (int)I('get.pid');
    		$up_info = ($pid==0) ? null : D('ServiceApi')->getAskCategory($pid);
    		$this->assign('info',null);
    		$this->assign('up_info',$up_info);
    		$this->assign('meta_title_c','文章分类管理');
    		$this->assign('meta_title','文章分类添加');
    		$this->display('categoryAdd');
    	}
    }
    
    /**
     * @todo 编辑分类
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function editCategory() {
        if(IS_POST){
            $this->updateCategory();
        }else{
            $id = (int)I('get.id',0);
            $obj = D('ServiceApi');
            $info = $obj->getAskCategory($id);
            if(empty($info)){
                $this->error('分类信息不存在');
            }
        	if($info['ask_category_pid']>0){
                $up_info = $obj->getAskCategory($info['ask_category_pid']);
            }else{
                $up_info = null;
            }
            $this->assign('info',$info);
            $this->assign('up_info',$up_info);
            $this->assign('meta_title_c','文章分类管理');
            $this->assign('meta_title','文章分类添加');
            $this->display('categoryAdd');
        }
    }
    
    /**
     * @todo 添加/编辑文章分类(仅供内部调用)
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    private function updateCategory(){
    	$ask_category_id = (int)I('ask_category_id',0);
    	$ask_category_pid = (int)I('ask_category_pid',0);
    	$obj = D('Admin/AskCategory');
    	$id = $obj->update();
    	if($id === false){
    		$this->error($obj->getError());
    	}else{
    		if($ask_category_id == 0){
    			action_log('article_category_add', 'askCategory', $id, UID);
    		}else{
    			action_log('article_category_edit', 'askCategory', $ask_category_id, UID);
    		}
    	    S('ask_category_list',null);
    	    S('ask_category_tree_list',null);
    		$this->success('操作成功', Cookie('__forward__'));
    	}
    }
    
    /**
     * @todo 分类状态修改
     * @param string $method 标识
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function changeCategory($method = null){
    	$id = array_unique((array)I('id',0));
    	$id = is_array($id) ? implode(',',$id) : $id;
    	if(empty($id)) {
    		$this->error('请选择要操作的数据!');
    	}
    	S('ask_category_list',null);
    	S('ask_category_tree_list',null);
    	$map[D('Admin/AskCategory')->getPk()] = array('in',$id);
    	switch (strtolower($method) ){
    		case 'forbid':
    			action_log('article_category_forbid', 'askCategory', $id, UID);
    			$this->forbid('AskCategory', $map );
    			break;
    		case 'resume':
    			action_log('article_category_resume', 'askCategory', $id, UID);
    			$this->resume('AskCategory', $map );
    			break;
    		case 'delete':
    			$obj_Category = D('Admin/AskCategory');
    			if($obj_Category->judgeAskCategory($id) === false){
    				$this->error('存在下级分类,请先删除下级分类！');
    			}
    			if($obj_Category->judgeArticleCategory($id) === false){
    				$this->error('分类与文章绑定，请先操作文章！');
    			}
    			if($obj_Category->judgeTagsCategory($id) === false){
    				$this->error('分类与标签绑定，请先操作标签！');
    			}
    			action_log('article_category_delete', 'askCategory', $id, UID);
    			$this->delete('AskCategory', $map);
    			break;
    		default:
    			$this->error('参数非法');
    	}
    }
    
    /**
     * @todo 标签列表
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function tags() {
    	$search_text = trim((string)strip_tags(I('search_text')));//搜索内容
    	$ask_category_id = (int)I('ask_category_id',0);
    	$ask_category_pid = (int)I('ask_category_pid',0);
    	$position = (int)I('position',0);
    	$ask_article = D('Admin/AskArticle');
    	$map = array();//初始化查询条件
    	if($position > 0){
    	    $map = D('AskTags')->listMap($position);
    	}
    	$map['ask_tags_id'] = array('gt',0);
	    if(!empty($search_text)){
    		$map['ask_tags_name'] = array('like', '%'.$search_text.'%');
    	}
	    if($ask_category_id > 0){
    		$map['ask_category_id'] = $ask_category_id;
    	}
    	$obj = D('ServiceApi');
    	//一级分类
    	$category_list = $obj->getAskCategoryTree();
    	$this->assign('category_list',$category_list);
    	//二级分类
    	if($ask_category_pid>0 && isset($category_list[$ask_category_pid])){
    	    $two_category_list = $category_list[$ask_category_pid]['_child'];
    	}else{
    	    $two_category_list = null;
    	}
    	$this->assign('two_category_list',$two_category_list);
    	
    	//列表分类
    	$category_info = $obj->getAskCategory();
    	$this->assign('category_info',$category_info);
    	//列表
    	$field = 'ask_tags_id,ask_tags_name,ask_tags_keyword,ask_tags_title,ask_tags_description,ask_category_id';
    	$list = $this->lists(D('AskTagsView'),$map,'ask_tags_id desc',$field);
    	$this->assign('_list',$list);
    	$this->assign('meta_title','文章标签管理');
    	Cookie('__forward__',$_SERVER['REQUEST_URI']);
    	$this->display();
    }
    
    /**
     * @todo 文章标签
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function addTags(){
    	if(IS_POST){
    		$ask_category_id = (array)I('ask_category_id');
    		$this->updateTags($ask_category_id);
    	}else{
    		$category_list = D('ServiceApi')->getAskCategoryTree();
    		$this->assign('info',null);//功能用一个页时需要给值null
    		$this->assign('category_list',$category_list);
    		$this->assign('meta_title','新增文章标签');
    		$this->assign('meta_title_c','标签列表');
    		$this->display('tagsAdd');
    	}
    }
    
    /**
     * @todo 编辑标签
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function editTags() {
    	$ask_tags_id = (int)I('id');
    	if(IS_POST) {
    		$ask_category_id = (array)I('ask_category_id');
    		$id = $this->updateTags($ask_category_id);
    	} else {
    		if($ask_tags_id == 0){
    			$this->error('参数传递错误');
    		}
    		$obj_ask = D('AskTags');
    		$info = $obj_ask->editAskTagsInfo($ask_tags_id);
    		if(false === $info){
    			$this->error($obj_ask->getError());
    		}
    		$obj = D('ServiceApi');
    		$category_data = $obj->getAskCategory($info['ask_category_id']);
    		if(empty($category_data)){
    			$this->error('分类信息不存在');
    		}
    		$info['ask_category_pid'] = $category_data['ask_category_pid'];
    		//一级分类
     		$category_list = $obj->getAskCategoryTree();
     		$this->assign('category_list',$category_list);
    		//二级分类
    		if($info['ask_category_pid']>0 && isset($category_list[$info['ask_category_pid']])){
    			$two_category_list = $category_list[$info['ask_category_pid']]['_child'];
    		}else{
    			$two_category_list = null;
    		}
    		$this->assign('two_category_list',$two_category_list);
    		$this->assign('info',$info);
    		$this->assign('meta_title','编辑标签');
    		$this->assign('meta_title_c','标签列表');
    		$this->display('tagsAdd');
    	}
    }
    
    /**
     * @todo 添加/编辑文章标签
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    private function updateTags($ask_category_id){
    	$ask_tags_id = (int)I('ask_tags_id');
    	$ask_category_ids = (array)$ask_category_id;
    	$obj = D('Admin/AskTags');
    	$update = $obj->update();
    	if($update === false){
    		$this->error($obj->getError());
    	}else{
    		$new_id = ($ask_tags_id == 0) ? $update: $ask_tags_id;
    		$change_relation = $obj->changeTagCategory($new_id,$ask_category_ids);
    		if($change_relation === false){
    			$this->error = '关系表更新出错，请重试！';
    		}
    		if($ask_tags_id == 0){
    			action_log('article_tags_add', 'askTags', $update, UID);
    		}else{
    			action_log('article_tags_edit', 'askTags', $ask_tags_id, UID);
    		}
    		S('ask_tags_list',null);
    		$this->success('操作成功',Cookie('__forward__'));
    	}
    }
    
    /**
     * @todo 文章标签状态修改
     * @param string $method 标识
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function changeAskTags($method = null){
    	$id = array_unique((array)I('id',0));
    	$id = is_array($id) ? implode(',',$id) : $id;
    	if(empty($id)) {
    		$this->error('请选择要操作的数据!');
    	}
    	$obj = D('Admin/AskTags');
    	$object = M('askCategoryTags');
    	$map[$obj->getPk()] = array('in',$id);
    	switch (strtolower($method) ){
    		case 'forbid':
    			$this->forbid($obj, $map );
    			break;
    		case 'resume':
    			$this->resume($obj, $map );
    			break;
    		case 'delete':
    			$delect_tags_category = $obj->delectTagsCategory($id);
    			if(false === $delect_tags_category){
    				$this->error = '删除关系表失败';
    			}
    			$delect_tags = $obj->where($map)->delete();
    			if(!$delect_tags){
    				$this->error = '删除失败';
    			}
    			action_log('article_tags_delete', 'askTags', $id, UID);
    			S('ask_tags_list',null);
    			$this->success('删除成功',Cookie('__forward__'));
    			break;
    		default:
    			$this->error('参数非法');
    	}
    }
    
	/**
     * @todo 文章列表
     */
    public function article() {
    	$search_text = trim((string)strip_tags(I('search_text')));//搜索内容
    	$ask_category_id = (int)I('ask_category_id',0);
    	$ask_category_pid = (int)I('ask_category_pid',0);
    	$position = (int)I('position',0);
    	$search_text = trim(strip_tags(I('search_text')));//搜索内容
    	$ask_article = D('Admin/AskArticle');
    	
    	$map = array();//初始化查询条件
    	if($position > 0){
    		$map = $ask_article->listMap($position);
    	}
    	if(!empty($search_text)){
    		$map['ask_article_title'] = array('like', '%'.$search_text.'%');
    	}
   		//分类
    	if($ask_category_pid > 0){
    		$map['ask_category_pid'] = $ask_category_pid;
    	}
    	if($ask_category_id > 0){
    		$map['ask_category_id'] = $ask_category_id;
    	}
    	
    	$map['status'] = array('gt',-1);
    	$field = 'ask_article_id,ask_article_title,status,create_time,update_time,sort';
    	$list = $this->lists('ask_article',$map,'sort desc,ask_article_id desc',$field);
    	$this->assign('_list',$list);
    	
    	$obj = D('ServiceApi');
    	//一级分类
    	$category_list = $obj->getAskCategoryTree();
    	$this->assign('category_list',$category_list);
    	//二级分类
    	if($ask_category_pid>0 && isset($category_list[$ask_category_pid])){
    	    $two_category_list = $category_list[$ask_category_pid]['_child'];
    	}else{
    	    $two_category_list = null;
    	}
    	$this->assign('two_category_list',$two_category_list);
    	Cookie('__forward__',$_SERVER['REQUEST_URI']);
    	$this->assign('meta_title','文章管理');
    	$this->display();
    }
    
    /**
     * @todo 新增文章
     */
    public function addArticle() {
    	if(IS_POST){
    		$this->updateArticle();
    	}else{
    	    $obj = D('ServiceApi');
    	    //一级分类
    	    $category_list = $obj->getAskCategoryTree();
    	    $this->assign('category_list',$category_list);
    		$this->assign('info',null);
    		$this->assign('meta_title','新增文章');
    		$this->assign('meta_title_c','文章列表');
    		$this->display('articleAdd');
    	}
    }
    
    /**
     * @todo 编辑文章
     */
    public function editArticle() {
    	if(IS_POST) {
    		$this->updateArticle();
    	} else {
    		$ask_article_id = (int)I('id');
	        if($ask_article_id == 0){
	            $this->error('参数传递错误');
	        }
	        $info = M('AskArticle')->where(array('ask_article_id'=>$ask_article_id))->find();
	        if(empty($info)){
	            $this->error('文章信息不存在');
	        }
	        
	        $obj = D('ServiceApi');
	        //一级分类
	        $category_list = $obj->getAskCategoryTree();
	        $this->assign('category_list',$category_list);
	        
	        //二级分类
	        if($info['ask_category_pid']>0 && isset($category_list[$info['ask_category_pid']])){
	            $two_category_list = $category_list[$info['ask_category_pid']]['_child'];
	        }else{
	            $two_category_list = null;
	        }
	        $this->assign('two_category_list',$two_category_list);
	        $this->assign('info',$info);
	        $this->assign('meta_title','编辑文章');
	        $this->assign('meta_title_c','文章列表');
	        $this->display('articleAdd');
	    }
    }
    
    /**
     * @todo 添加/编辑文章标签
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    private function updateArticle(){
    	$ask_article_id = (int)I('ask_article_id');
    	$obj = D('Admin/AskArticle');
    	$id = $obj->update();
    	if($id === false){
    		$this->error($obj->getError());
    	}else{
    		if($ask_article_id == 0){
    			action_log('article_add', 'askArticle', $id, UID);
    		}else{
    			action_log('article_edit', 'askArticle', $ask_article_id, UID);
    		}
    		S('ask_article_new_list',null);
    		S('ask_article_detail',null);
    		$this->success('操作成功',Cookie('__forward__'));
    	}
    }
    
    /**
     * @todo 文章状态修改
     * @param string $method 标识
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function changeArticle($method = null){
    	$id = array_unique((array)I('id',0));
    	$id = is_array($id) ? implode(',',$id) : $id;
    	if(empty($id)) {
    		$this->error('请选择要操作的数据!');
    	}
    	S('ask_article_new_list',null);
    	S('ask_article_detail',null);
    	$map[D('Admin/AskArticle')->getPk()] = array('in',$id);
    	switch (strtolower($method) ){
    		case 'forbid':
    			action_log('article_forbid', 'askArticle', $id, UID);
    			$this->forbid('AskArticle', $map );
    			break;
    		case 'resume':
    			action_log('article_resume', 'askArticle', $id, UID);
    			$this->resume('AskArticle', $map );
    			break;
    		case 'delete':
    			action_log('article_delete', 'askArticle', $id, UID);
    			$this->delete('AskArticle', $map);
    			break;
    		default:
    			$this->error('参数非法');
    	}
    }
    
    /**
     * @todo 关键字链接列表
     */
    public function keyWordLink() {
        $name = (string)I('get.name','');
        cookie('__forward__',$_SERVER['REQUEST_URI']);
        $map = array();
        if(!empty($name)){
            $map['ask_keyword_name'] = array('like','%'.$name.'%');
        }
        $list = $this->lists('ask_keyword_link',$map);
        $this->assign('_list',$list);
        $this->assign('meta_title','关键字管理');
        $this->display();
    }
    
    /**
     * @todo 添加关键字链接
     */
    public function addKeyWordLink() {
        if(IS_POST){
            $obj = D('AskKeywordLink');
            $id = $obj->update();
            if(false === $id){
                $this->error($obj->getError());
            }else{
            	action_log('ask_keyword_link_add', 'askKeywordLink', $id, UID);
                S('ask_keyword_link_list',null);
                $this->success('添加成功！', cookie('__forward__'));
            }
        }
        $this->assign('meta_title','添加关键字');
        $this->display('editKeyWordLink');
    }
    
    /**
     * @todo 删除关键字
     */
    public function deleteKeyWordLink() {
        $ids    =   I('request.ids');
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }
        
        $obj = D('AskKeywordLink');
        $map['ask_keyword_id'] = array('in',$ids);
        if(false === $obj->where($map)->delete()){
            $this->error($obj->getError());
        }else{
        	action_log('ask_keyword_link_delete', 'askKeywordLink', $ids, UID);
            S('ask_keyword_link_list',null);
            $this->success('删除成功');
        }
    }
}